package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.SecurityDao;

import net.sf.json.JSONObject;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年08月16日 上午13:25:42
 * @功能：密保功能
 */
@Repository
public class SecurityDaoImpl  extends P2PJdbcDao implements SecurityDao {
	

	Logger logger = Logger.getLogger(SmrzjlDaoImpl.class);
	
	/**
	 * 
	 * @author qjy
	 * @version 创建时间：2016年08月16日 上午15:25:42
	 * @功能：加载密保问题
	 */

	@Override
	public List searchSecurity(JSONObject params){
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append("select ID, SECURITY_QUESTION ");
		sql.append(" from t_zhfw_security_question");
		sql.append(" where 1=1");
		logger.info("querySecurity"+sql.toString());
		logger.info("querySecurity paramMap is "+paramMap);
	return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}
	/**
	 * @author qjy
	 * @version 创建时间爱:2016年07月16日15:30:33
	 * @功能:保存密保问题及答案
	 * */
	@Override
	public int saveSecurity(JSONObject parms) {
		String id = parms.optString("id");
		String username = parms.optString("username");
		String squestion = parms.optString("squestion");
		String sanswer = parms.optString("sanswer");
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO `t_fm_sys_role`(`ID`,`USER_NAME`,`SECURITY_QUESTION`,`SECURITY_ANSWER`) values(:id,:username,:squestion,:sanswer) ");
		SqlParameterSource parmsMap = new MapSqlParameterSource().addValue("id", id).addValue("username", username)
				.addValue("squestion", squestion).addValue("sanswer", sanswer);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap);
	}
	/**
	 * @author qjy
	 * @version 创建时间爱:2016年07月16日15:40:33
	 * @功能:加载用户密保问题?根据用户名查询
	 * */
	@Override
	
	public List<Map<String, Object>> searchSecuritylist(JSONObject params){
				int start = params.optInt("start");
				int limit = params.optInt("limit");
				String username = params.optString("username");
				StringBuffer sql = new StringBuffer();
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("START", start);
				paramMap.put("LIMIT", limit);
				paramMap.put("username", username);
				sql.append("select ID,USER_NAME,SECURITY_QUESTION,SECURITY_ANSWER ,REF_QUESTION_ID");
				sql.append(" from t_zhfw_security");
				sql.append(" where 1=1");
				if(StringUtils.isNotEmpty(username)){
					sql.append(" AND USER_NAME like "+ "'%"+username+"%'");
				}
				logger.info("gettjrCount sql is " + sql.toString());
				logger.info("gettjrCount paramMap is " + paramMap.toString());
				return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);

	}
	
	
	
	
	@Override
	public List querySanswer(JSONObject params) {
		String id = params.optString("id");
		String squestion_one = params.optString("squestion_one");
		String sanswer_one = params.optString("sanswer_one");
		String squestion_two = params.optString("squestion_two");
		String sanswer_two = params.optString("sanswer_two");
		String squestion_three = params.optString("squestion_three");
		String sanswer_three = params.optString("sanswer_three");
		StringBuffer sql = new StringBuffer();
		sql. append("select ID,USER_NAME,SECURITY_QUESTION,SECURITY_ANSWER");
		sql.append(" from t_zhfw_security");
		sql.append(" where 1=1");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		if (StringUtils.isNotEmpty(squestion_one)) {
			sql.append(" AND USER_NAME =:squestion_one");
			paramMap.put("squestion_one", squestion_one);
		}
		if (StringUtils.isNotEmpty(sanswer_one)) {
			sql.append(" AND USER_NAME =:sanswer_one");
			paramMap.put("sanswer_one", sanswer_one);
		}
		if (StringUtils.isNotEmpty(squestion_two)) {
			sql.append(" AND USER_NAME =:squestion_two");
			paramMap.put("squestion_two", squestion_two);
		}
		if (StringUtils.isNotEmpty(sanswer_two)) {
			sql.append(" AND USER_NAME =:sanswer_two");
			paramMap.put("sanswer_two", sanswer_two);
		}
		if (StringUtils.isNotEmpty(squestion_three)) {
			sql.append(" AND USER_NAME =:squestion_three");
			paramMap.put("squestion_three", squestion_three);
		}
		if (StringUtils.isNotEmpty(sanswer_three)) {
			sql.append(" AND USER_NAME =:sanswer_three");
			paramMap.put("sanswer_three", sanswer_three);
		}
		logger.info("querySecuritylist"+sql.toString());
		logger.info("querySecuritylist paramMap is "+paramMap);
	return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}
	
	
	
	@Override
	public int getSecurity(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String username = params.optString("username");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("username", username);
		
		sql.append(" SELECT count(*)  ");
		sql.append(" FROM t_zhfw_security");
		sql.append("   where 1=1 ");
		
		if(StringUtils.isNotEmpty(username)){
			sql.append(" AND USER_NAME like "+ "'%"+username+"%'");
		}
		logger.info("gettjrCount sql is " + sql.toString());
		logger.info("gettjrCount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}


	
}
